package com.gaofeng.algo.java;

import java.util.Deque;
import java.util.LinkedList;

public class SortJava {

    public int maxAreaOfIsland(int[][] grid) {
        if (grid.length == 0) {
            return 0;
        }
        int answer = 0;
        int m = grid.length;
        int n = grid[0].length;
        int[] directions = new int[]{-1, 0, 1, 0, -1};
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                int area = 0;
                Deque<Integer> stackI = new LinkedList<>();
                Deque<Integer> stackJ = new LinkedList<>();
                stackI.push(i);
                stackJ.push(j);
                while (!stackI.isEmpty()) {
                    Integer curri = stackI.pop();
                    Integer currj = stackJ.pop();
                    //搜索条件判断
                    if (curri < 0 || currj < 0 || curri == m || currj == n || grid[curri][currj] != 1) {
                        continue;
                    }
                    //状态标记
                    grid[curri][currj] = 0;
                    area++;
                    for (int k = 0; k < 4; k++) {
                        int nextI = curri + directions[k];
                        int nextJ = currj + directions[k + 1];
                        stackI.push(nextI);
                        stackJ.push(nextJ);
                    }
                    answer = Math.max(answer, area);
                }
            }
        }
        return answer;
    }

    /**
     * 查找朋友圈的个数
     * 解法其实是求连通分量的个数
     *
     * @param M
     * @return
     */
    public int findCircleNum(int[][] M) {
        int size = M.length;
        boolean[] visited = new boolean[size];
        int count = 0;
        for (int i = 0; i < size; i++) {
            if (!visited[i]) {
                dfs(M, i, visited);
                count++;
            }
        }
        return count;
    }

    private void dfs(int[][] m, int i, boolean[] visited) {
        for (int j = 0; j < m.length; j++) {
            if (!visited[j] && m[i][j] == 1) {
                visited[j] = true;
                dfs(m, j, visited);
            }
        }
    }


    public int findKthLargest(int[] numbers, int k) {
        int lo = 0;
        int hi = numbers.length - 1;
        int target = hi - k + 1;
        while (lo < hi) {
            int partition = partition(numbers, lo, hi);
            if (partition == target) {
                return numbers[partition];
            }
            if (partition < target) {
                lo = partition + 1;
            } else {
                hi = partition - 1;
            }
        }
        return numbers[lo];
    }

    private int partition(int[] numbers, int lo, int hi) {
        int i = lo + 1;
        int j = hi;
        while (true) {
            //注意如果是小于等于也要继续往后移动指针
            while (i < hi && numbers[i] <= numbers[lo]) {
                i++;
            }
            while (j > lo && numbers[j] >= numbers[lo]) {
                j--;
            }
            if (i >= j) {
                break;
            }
            swap(numbers, i, j);
        }
        swap(numbers, lo, j);
        return j;
    }

    private void swap(int[] numbers, int i, int j) {
        int temp1 = numbers[i];
        numbers[i] = numbers[j];
        numbers[j] = temp1;
    }
}
